
<template>
  <div class="home">
    <TabControl @tabClick='tabClick' v-show='isShowNav' ref="tabControlRef1" class='tab-control' />
    <Scroll ref="scrollRef" class='container' @scroll='contentScroll' :probe-type='3' :pullUpLoad='true' @pullingUp='loadMore'>
      <HomeSwiper :banners='banners' @imgLoad='swiperImageLoad' />
      <RecommendView :recomends='recomends' />
      <FeatureView />
      <TabControl @tabClick='tabClick' ref="tabControlRef2" />
      <GoodList :goods='showGoods' />
    </Scroll>
    <BackTop v-show='isBackShow' @click.native='backTopClick' />
  </div>
</template>

<script>
import TabControl from 'components/content/tabControl'
import Scroll from 'components/common/scroll/Scroll'
import HomeSwiper from './childComps/HomeSwiper'
import FeatureView from './childComps/FeatureView'
import RecommendView from './childComps/RecommendView'

import GoodList from 'components/content/goods/GoodsList'
import { getHomeMuticata, getHomegoods } from 'network/home'
import { itemMixin } from 'common/mixin'
export default {
  name: 'homeIndex',
  components: { TabControl, Scroll, HomeSwiper, RecommendView, FeatureView, GoodList },
  data() {
    return {
      banners: [],
      recomends: [],
      goods: {
        pop: {
          page: 0,
          list: []
        },
        new: {
          page: 0,
          list: []
        },
        sell: {
          page: 0,
          list: []
        }
      },
      curType: 'pop',
      offsetArr: [],
      tabOffsetTop: 0,
      isShowNav: false,
      saveY: 0
    }
  },
  mixins: [itemMixin],
  created() {
    this.loadHomeMuticata()
    this.loadHomegoods('pop')
    this.loadHomegoods('new')
    this.loadHomegoods('sell')
  },
  computed: {
    showGoods() {
      return this.goods[this.curType].list
    }
  },
  activated() {
    console.log(this.saveY)
    this.$refs.scrollRef.scrollTo(0, this.saveY, 30)
  },
  deactivated() {
    this.saveY = this.$refs.scrollRef.getSaveY()
    console.log(this.saveY)
    this.$bus.$off('goodsImageLoad', this.itemListener)
  },
  methods: {
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControlRef2.$el.offsetTop
    },
    contentScroll(y) {
      this.isBackShow = y > 1000
      this.isShowNav = y > this.tabOffsetTop
    },
    loadMore() {
      this.loadHomegoods(this.curType)
      this.$refs.scrollRef.refresh()
    },
    tabClick(i) {
      switch (i) {
        case 0:
          this.curType = 'pop'
          break
        case 1:
          this.curType = 'new'
          break
        case 2:
          this.curType = 'sell'
          break
      }
      this.$refs.tabControlRef2.curIdx = i
      this.$refs.tabControlRef1.curIdx = i
    },
    loadHomeMuticata() {
      getHomeMuticata().then(res => {
        this.banners = res.data.banner.list /*轮播图数据*/
        this.recomends = res.data.recommend.list /*  */
      })
    },
    loadHomegoods(type) {
      let item = this.goods[type]
      item.page++
      getHomegoods(type, item.page).then(res => {
        item.list.push(...res.data.list)
        this.$refs.scrollRef.finishPullUp()
      })
    }
  }
}
</script>

<style scoped>
.home {
  position: relative;
  background-color: #fff;
  height: calc(100vh - 44px);
}
.tab-control {
  position: relative;
  z-index: 5;
}
.container {
  position: absolute;
  overflow: hidden;
  top: 0px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
